﻿Public Class program
    Private Shared MainWindow As frmTimes

    Public Shared Function Main() As Integer
        Dim scheduler As AppScheduler = Config.ReadScheduler()
        If scheduler.Count = 0 Then
            Return 1
        End If

        Dim trayIcon As New NotifyIcon()
        trayIcon.Icon = SystemIcons.Application
        trayIcon.Visible = True
        AddHandler trayIcon.MouseClick, AddressOf trayIcon_MouseUp

        MainWindow = New frmTimes

        Dim timer As New Timer()
        timer.Interval = 1000
        AddHandler timer.Tick, AddressOf timer_tick
        timer.Tag = scheduler
        timer.Start()

        AddHandler MainWindow.ExecuteRequest, AddressOf ExecuteRequested

        Try
            MainWindow.Init(scheduler)
            MainWindow.Visible = False
            Application.Run(MainWindow)
        Catch ex As Exception
            Debug.WriteLine(ex.Message)
            Return 2
        Finally
            trayIcon.Dispose()
        End Try
        Return 0
    End Function

    Private Shared Sub trayIcon_MouseUp(ByVal sender As Object, ByVal e As MouseEventArgs)
        If e.Button = MouseButtons.Left Then
            If MainWindow.Visible Then
                MainWindow.Hide()
            Else
                Dim pos As New Point
                pos.X = My.Computer.Screen.WorkingArea.Right - MainWindow.Width
                pos.Y = My.Computer.Screen.WorkingArea.Bottom - MainWindow.Height
                MainWindow.Location = New Point(pos)
                MainWindow.AllowShow()
                MainWindow.Show()
            End If
        ElseIf e.Button = MouseButtons.Right Then

        End If
    End Sub

    Private Shared ticks As Integer = 0
    Private Shared Sub timer_tick(ByVal sender As Object, ByVal e As EventArgs)
        If MainWindow Is Nothing Then
            Return
        End If
        If MainWindow.IsDisposed Then
            Return
        End If
        If MainWindow.Destroyed Then
            Return
        End If
        Try

            ticks += 1

            Dim scheduler As AppScheduler = CType(CType(sender, Timer).Tag, AppScheduler)
            Debug.WriteLine(ticks)

            Dim tdef As TimeDef = scheduler.Tick()
            If tdef Is Nothing Then
                MainWindow.Close()
                Return
            End If
            MainWindow.UpdateTimer(tdef)
            If tdef.TimeLeft <= 0 Then
                Execute(tdef)
                If scheduler.Count = 0 Then
                    MainWindow.Close()
                End If
            End If
        Catch ex As Exception
            Console.WriteLine(ex.Message)
        End Try
    End Sub

    Private Shared Sub Execute(ByVal tdef As TimeDef)
        If tdef.Active Then
            tdef.Done()
            MainWindow.SetExecuted(tdef.Name)
            Debug.WriteLine(CStr(ticks) & " Executing: " & tdef.Name & ": " & tdef.Path)
            Dim proc As New Process()
            proc.StartInfo.UseShellExecute = False
            proc.StartInfo.FileName = tdef.Path
            proc.StartInfo.Arguments = Join(tdef.Args, " ")

            proc.Start()
        End If
    End Sub
    'def execute(tdef):
    'global mainWindow
    'tdef.done()
    'mainWindow.set_executed(tdef.name())
    'print str(_ticks) + " Executing: " + tdef.name() +": " + tdef.path()
    'subprocess.Popen(shlex.split(tdef.path()),
    '        stdout=subprocess.PIPE, 
    '        stderr=subprocess.PIPE, 
    '        stdin=subprocess.PIPE)

    Private Shared Sub ExecuteRequested(ByVal tdef As TimeDef)
        Execute(tdef)
    End Sub
End Class
